<template>
  <el-dialog
    :close-on-click-modal="false"
    :model-value="api.createState.visible"
    :title="api.createState.title"
    destroy-on-close
    width="80%"
    @close="api.onCloseCreate()"
  >
    <el-form
      ref="formRef"
      :model="api.createState.form"
      :rules="rules"
      label-width="80px"
      status-icon
    >
      <el-row>
        <el-col :span="8">
          <el-form-item
            label="包路径"
            prop="packageName"
          >
            <el-input
              v-model="api.createState.form.packageName"
              clearable
              placeholder="包路径"
            />
          </el-form-item>

          <el-form-item
            label="表名"
            prop="tableName"
          >
            <el-input
              v-model="api.createState.form.tableName"
              clearable
              placeholder="表名"
            />
          </el-form-item>
        </el-col>
        <el-col :span="8">

          <el-form-item
            label="表描述"
            prop="tableComment"
          >
            <el-input
              v-model="api.createState.form.tableComment"
              clearable
              placeholder="表描述"
            />
          </el-form-item>
          <el-form-item
            label="模块名"
            prop="moduleName"
          >
            <el-input
              v-model="api.createState.form.moduleName"
              clearable
              placeholder="模块名"
            />
          </el-form-item>
        </el-col>
        <el-col :span="8">

          <el-form-item
            label="类名"
            prop="className"
          >
            <el-input
              v-model="api.createState.form.className"
              clearable
              placeholder="类名"
            />
          </el-form-item>

          <el-form-item
            label="request"
            prop="requestMapping"
          >
            <el-input
              v-model="api.createState.form.requestMapping"
              clearable
              placeholder="requestMapping"
            />
          </el-form-item>
        </el-col>
        <el-col :span="8">

          <el-form-item
            label-width="120"
            label="vueApiPath"
            prop="vueApiPath"
          >
            <el-input
              v-model="api.createState.form.vueApiPath"
              clearable
              placeholder="vueApiPath"
            />
          </el-form-item>

          <el-form-item
            label-width="120"
            label="vueApiName"
            prop="vueApiName"
          >
            <el-input
              v-model="api.createState.form.vueApiName"
              clearable
              placeholder="vueApiName"
            />
          </el-form-item>
        </el-col>

        <el-col :span="8">

          <el-form-item
            label-width="120"
            label="vuePagePath"
            prop="vuePagePath"
          >
            <el-input

              v-model="api.createState.form.vuePagePath"
              clearable
              placeholder="vuePagePath"
            />
          </el-form-item>

          <el-form-item
            label-width="120"
            label="vuePageName"
            prop="vuePageName"
          >
            <el-input
              v-model="api.createState.form.vuePageName"
              clearable
              placeholder="vuePageName"
            />
          </el-form-item>
        </el-col>

      </el-row>
      <div class="d-flex flex-column">
        <el-space class="mb-3">
          <el-button plain type="primary" @click="addField">增加字段</el-button>
        </el-space>
        <el-table
          :data="api.createState.form.fields"
          border
        >
          <el-table-column :width="180" label="类型" prop="type">
            <template #default="{ row, column, $index }">
              <el-select v-if="api.createState.form.fields" v-model="api.createState.form.fields[$index].type">
                <el-option label="String" value="java.lang.String" />
                <el-option label="Long" value="java.lang.Long" />
                <el-option label="Integer" value="java.lang.Integer" />
                <el-option label="Boolean" value="java.lang.Boolean" />
                <el-option label="BigInteger" value="java.math.BigInteger" />
                <el-option label="Float" value="java.lang.Float" />
                <el-option label="Double" value="java.lang.Double" />
                <el-option label="Short" value="java.lang.Short" />
                <el-option label="BigDecimal" value="java.math.BigDecimal" />
                <el-option label="Date" value="java.util.Date" />
                <el-option label="Timestamp" value="java.sql.Timestamp" />
                <el-option label="Time" value="java.sql.Time" />
                <el-option label="long" value="long" />
                <el-option label="int" value="int" />
                <el-option label="boolean" value="boolean" />
                <el-option label="float" value="float" />
                <el-option label="double" value="double" />
                <el-option label="short" value="short" />
                <el-option label="char" value="char" />
              </el-select>
            </template>
          </el-table-column>

          <el-table-column label="名称" prop="name">
            <template #default="{ row, column, $index }">
              <el-input
                v-if="api.createState.form.fields"
                v-model="api.createState.form.fields[$index].name"
                clearable
                placeholder="名称"
              />
            </template>
          </el-table-column>

          <el-table-column :width="80" label="exist" prop="exist">
            <template #default="{ row, column, $index }">
              <el-switch v-if="api.createState.form.fields" v-model="api.createState.form.fields[$index].exist" />
            </template>
          </el-table-column>

          <el-table-column label="备注" prop="commentComment">
            <template #default="{ row, column, $index }">
              <el-input
                v-if="api.createState.form.fields"
                v-model="api.createState.form.fields[$index].commentComment"
                clearable
                placeholder="备注"
              />
            </template>
          </el-table-column>

          <el-table-column align="center" label="绑定" prop="action">
            <template #default="{ row, column, $index }">
              <BindVo :api="api" :field="api.createState.form.fields?.[$index]" @submit="onBindVo" />
            </template>
          </el-table-column>

          <el-table-column align="center" label="删除" prop="action" width="70">
            <template #default="{ row, column, $index }">
              <el-button
                size="small"
                text
                type="danger"
                @click="onDeleteField($index)"
              >
                <template #icon>
                  <el-icon>
                    <Delete />
                  </el-icon>
                </template>
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>

      <el-form-item class="mb-0 mt-4" label-width="0">
        <el-button type="primary" @click="onSubmit">创建</el-button>
        <el-button @click="api.onCloseCreate()">关闭</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script lang="ts" setup>
import { GenTableApi } from "~/api/system/gen/gen.api";
import { reactive, ref } from "vue";
import { FormRules } from "element-plus";
import BindVo from "~/views/config/gen/BindVo.vue";
import { Delete } from "@element-plus/icons-vue";

interface Props {
  api: GenTableApi;
}

const formRef = ref();
const { api } = withDefaults(defineProps<Props>(), {});
const rules = reactive<FormRules>({
  packageName: [{ required: true, message: "请输入包路径", trigger: "blur" }],
  tableName: [{ required: true, message: "请输入表名", trigger: "blur" }],
  tableComment: [{ required: true, message: "请输入表描述", trigger: "blur" }],
  moduleName: [{ required: true, message: "请输入模块名", trigger: "blur" }],
  className: [{ required: true, message: "请输入类名", trigger: "blur" }],
  requestMapping: [{ required: true, message: "请输入requestMapping", trigger: "blur" }],
});

function addField() {
  if (!api.createState.form.fields) {
    api.createState.form.fields = [];
  }
  api.createState.form.fields.push({
    type: "String",
    name: "test",
    exist: true,
    commentComment: "备注",
  });
}

async function onBindVo(e: any) {
  if (!api.createState.form.voFields) {
    api.createState.form.voFields = [];
  }
  api.createState.form.voFields.push(e);
}

async function onDeleteField(index: number) {
  api.createState.form.fields?.splice(index, 1);
}

async function onSubmit() {
  const values = await formRef.value.validate();
  if (values) {
    api.submit(api.createState.form).then((res: any) => {
      if (res.ok) {
        api.onCloseCreate();
      }
    });
  }
}
</script>

<style lang="scss" scoped>

</style>
